package com.jch.fs.server;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.LineBasedFrameDecoder;
import io.netty.handler.codec.string.StringDecoder;
import io.netty.handler.codec.string.StringEncoder;
import io.netty.handler.stream.ChunkedWriteHandler;

import java.nio.charset.StandardCharsets;

public class FSChannelInit extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        pipeline.addLast(new LineBasedFrameDecoder(1024)); // 基于换行符 \r\n
        pipeline.addLast(new ChunkedWriteHandler()); // 流量分块 状态管理
        pipeline.addLast(new FsServerChunkHandler()); // 自定义出站 分块处理 大数据块 处理器

        pipeline.addLast(new StringDecoder(StandardCharsets.UTF_8)); // 字符串解码器
        pipeline.addLast(new StringEncoder(StandardCharsets.UTF_8)); // 字符串编码器

        pipeline.addLast(new FsServerHandler());
    }
}
